package main

import (
	"fmt"
	"github.com/pebbe/zmq4"
	"log"
	// "math"
	// "math/rand"
	"time"
	// "time"
)

func main() {
	log.SetFlags(log.Lshortfile)
	ce := func(err error) {
		if err != nil {
			log.Panic(err)
		}
	}
	server := func() {
		context, err := zmq4.NewContext()
		ce(err)
		publisher, err := context.NewSocket(zmq4.PUB)
		ce(err)
		err = publisher.Bind("tcp://0.0.0.0:5555")
		ce(err)
		err = publisher.Bind("ipc://weather.ipc")
		ce(err)
		i := 0
		for {
			_, err := publisher.SendMessage(fmt.Sprintf("send %d", i))
			ce(err)
			i++
			time.Sleep(time.Second)
		}
	}
	i := 0
	client := func() {
		context, err := zmq4.NewContext()
		ce(err)
		subscriber, err := context.NewSocket(zmq4.SUB)
		ce(err)
		err = subscriber.Connect("tcp://127.0.0.1:5555")
		ce(err)
		ce(subscriber.SetSubscribe(""))
		for {
			i++
			if i%1000 == 0 {
				log.Println(i)
			}
			res, err := subscriber.RecvMessage(zmq4.SNDMORE)
			ce(err)
			log.Println("client recv", res)
		}
	}
	go client()
	go client()
	server()
}
